﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Model;
using AspNetCs.Bll;
using System.Data;
using System.Collections;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.memorial_enum;
using AspNetCs.Enumerations.User_Enum;
using AspNetCs.Common;
using AspNetCs.Enumerations.Channel_Enum;
using AspNetCs.Config;

public partial class Users_CreateHappyPet : BasePage
{
    HapppNetBLL bll = new HapppNetBLL();
    protected AspNetCs.Bll.Temple tempBll = new Temple();
    private int UserId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateHappyPet.aspx", "请您登录", true);
        UserId = Base_GetUserInfo.UserId;
        if (!IsPostBack)
        {
            int Point = (int)Base_GetUserInfo.Point;
            if (Point < configInfo.HappyPet)
            {
                //ResultStr("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateAncestralHall.Aspx", "您的可用点数:" + Point + ",创建宗祠需要" + inf.ancestralhallPoint+ "点,请您及时充值");
                ResultStr("Default.aspx", "您的可用点数:" + Point + ",创建宠物乐园需要" + configInfo.HappyPet + "点,请您及时充值");
            }
            MusicBLL musicBLL = new MusicBLL();
            musicBLL.BindMusic(ddlMusic);
        }
    }



    public HappyPetModel getModel()
    {
        Users_Model userModel = Base_GetUserInfo;

        HappyPetModel model = new HappyPetModel();
        model.PetName = txtPetName.Text;
        model.PetPhoto = Txt_UpImg.Text;
        model.Content = WebEditor1.Text;
        model.IsCheck = 0;
        model.IsDel = 0;
        model.TempleId = StringHelper.isNumber_int(Request["templateId"]);
        model.AcceeCount = 0;
        if (userModel.UserId>0)
        {
            model.CreateId = userModel.UserId;
        }
        model.CreateTime = DateTime.Now;
        model.MusicId = StringHelper.isNumber_int(ddlMusic.SelectedValue);

        return model;
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {

        HappyPetModel model = getModel();
        bll.AddHappyPet(model);
        DrePoint();
        ResultStr("HappyPetList.aspx", "添加成功!");
    }

    /// <summary>
    /// 扣除点数
    /// </summary>
    protected void DrePoint()
    {
        Users userBll = Users_Bll.CreatNew;
        int UserId = Base_GetUserInfo.UserId;
        decimal TemplatePoint = StringHelper.isNumber_decimal(Request["hldTemplePoint"]);
        int TempId = StringHelper.isNumber_int(Request["templateId"]);
        if (UserId != 0)
        {
            //if (TemplatePoint != 0)
            //{
            //    userBll.DrePoint(TemplatePoint, UserId);
            //}
            if ((TemplatePoint + configInfo.HappyPet) <= Base_GetUserInfo.Point)
            {
                Temple_Model TmpMd = TempleBll.CreateNew.GetModel(TempId);
                Point_Log_Model model = new Point_Log_Model();
                model.NetIp = StringHelper.Ip();
                model.Point = TemplatePoint + configInfo.HappyPet;
                if (TemplatePoint != 0)
                {
                    model.Readme = "宠物乐园扣除点数:" + configInfo.HappyPet + "点,选择模板[" + TmpMd.TempleName+ "]扣除" + TemplatePoint + "点";
                }
                else
                {
                    model.Readme = "宠物乐园扣除点数:" + configInfo.HappyPet + "点,选择了免费模板[" + TmpMd.TempleName+ "]";
                }

                model.UserId = this.UserId;
                model.UserType = (int)UserTypeEnum.User;
                model.Addtime = DateTime.Now;

                Point_Log_Bll.CreateNew.Add(model);
            }
            else
            {
                Response.Write("<script>alert('无法创建,用户点数不足(创建需要" + (TemplatePoint + configInfo.HappyPet) + "点,您只有" + Base_GetUserInfo.Point + "点)!');location.href='CreateHappyPet.aspx';</script>");
                return;
            }
            
        }
        else
        {
            CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateAncestralHall.Aspx", "请您登录", true);
        }
    }
}
